//
//  MapHeadView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/9/25.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol MapHeadViewDelegate {
    @objc optional func selectTravel(type:String) //选择出行方式
}

class MapHeadView: UIView {

    weak var mapDelegate : MapHeadViewDelegate?
    
    fileprivate let cellId = "mapCell"
    var dataArray = [driving,bus,walk,riding]
    
    var collectionView : UICollectionView!
    
    init() {
        super.init(frame: .zero)
        
        self.backgroundColor = UIColor.clear
        
        let layout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets.init(top: 10, left: 10, bottom: 10, right: 10)
        layout.itemSize = .init(width: ScreenW/5, height: RATIO_H(maxNum: 40))
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.clear
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(TypeCell.self, forCellWithReuseIdentifier: cellId)
        self.addSubview(collectionView)
        
        collectionView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.zero)
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension MapHeadView : UICollectionViewDelegate,UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as? TypeCell
        if indexPath.row == 0 {
            cell?.contentView.backgroundColor = UIColor.gray
        }else{
            cell?.contentView.backgroundColor = UIColor.editColor()
        }
        cell?.titleLabel.text = dataArray[indexPath.row]
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        if indexPath.row != 0 {
            let cell = collectionView.cellForItem(at: IndexPath.init(row: 0, section: 0)) as? TypeCell
            cell?.contentView.backgroundColor = UIColor.editColor()
        }
      
        let cell = collectionView.cellForItem(at: indexPath) as? TypeCell
        cell?.contentView.backgroundColor = UIColor.gray
        
        self.mapDelegate?.selectTravel!(type: dataArray[indexPath.row])
    }
    
    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
        
        let cell = collectionView.cellForItem(at: indexPath) as? TypeCell
        cell?.contentView.backgroundColor = UIColor.editColor()
    }
    
}
